const Taf = require('@taf/taf-rpc').client;
const News = require('./CrawlTestProxy').News;

Taf.setProperty('timeout', 60000);
function newsHelper(sBusId, opt) {
    // let strPrx = global.CONFIG.proxy.NewCrawlerObj;
    let options = opt || {};
    let strPrx = 'News.NewCrawlerServer.NewCrawlerObj';
    if (options.strPrx) {
        strPrx = options.strPrx;
    } else if (!process.env.TAF_CONFIG) { // 本地环境
        strPrx += '@tcp -h 172.16.8.125 -t 60000 -p 8888'; // 测试环境的代理ip跟端口
    }

    if (!process.env.TAF_CONFIG) { // 非taf环境下，加上ip
        // strPrx += '@tcp -h 172.16.8.125 -t 60000 -p 8888';
    }
    console.log('news-strPrx------------------------------------------', strPrx);

    let rpcObj = Taf.stringToProxy(News.CrawlTestProxy, strPrx);
    // rpcObj.setTimeout(60000);
    return {
        testRule(sUrl, sConfigId, sStationId) {
            let reqObj = new News.TestRuleReq();
            reqObj.readFromObject({
                sUrl, sConfigId, sStationId
            });
            return new Promise((resolve, reject) => {
                rpcObj.testRule(reqObj).then((rsp) => {
                    let result = rsp.response.arguments.rsp;
                    console.log('rsp-----', result);
                    resolve(result);
                }).catch((err) => {
                    let msg = err;
                    if (err && err.response && err.response.error) {
                        msg = err.response.error;
                    }
                    reject(new Error(`testRule Error: ${JSON.stringify(msg)}`));
                });
            });
        },
        getTestRuleResult(sUrl, sConfigId, sStationId) {
            let reqObj = new News.TestRuleReq();
            reqObj.readFromObject({
                sUrl, sConfigId, sStationId
            });
            return new Promise((resolve, reject) => {
                rpcObj.getTestRuleResult(reqObj).then((rsp) => {
                    let result = rsp.response.arguments.rsp;
                    console.log('rsp-----', result);
                    resolve(result);
                }).catch((err) => {
                    let msg = err;
                    if (err && err.response && err.response.error) {
                        msg = err.response.error;
                    }
                    reject(new Error(`getTestRuleResult Error: ${JSON.stringify(msg)}`));
                });
            });
        },
        saveTestRuleResult(sResultContent) {
            let reqObj = new News.SaveTestRuleResultReq();
            reqObj.readFromObject({
                sResultContent
            });
            return new Promise((resolve, reject) => {
                rpcObj.saveTestRuleResult(reqObj).then((rsp) => {
                    let result = rsp.response.arguments.rsp;
                    console.log('rsp-----', result);
                    resolve(result);
                }).catch((err) => {
                    let msg = err;
                    if (err && err.response && err.response.error) {
                        msg = err.response.error;
                    }
                    reject(new Error(`saveTestRuleResult Error: ${JSON.stringify(msg)}`));
                });
            });
        }
    };
}
module.exports = newsHelper;
